package com.czy.owner.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import com.czy.owner.R;
import com.czy.owner.entity.OnLineOrderServiceModel;

import java.util.List;


/**
 * Created by wangyang
 */
public class OrderServiceSubAdapter extends BaseRecyclerAdapter<OnLineOrderServiceModel.DataBean.SubsBean, OrderServiceSubAdapter.ItemServiceHolder> {
    private Context ctx;

    public OrderServiceSubAdapter(Context context, List<OnLineOrderServiceModel.DataBean.SubsBean> data) {
        super(context, data);
        this.ctx = context;
    }

    @Override
    public void onBindViewHolder(ItemServiceHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position));
        holder.rbItemServiceName.setId(position);
        holder.rbItemServiceName.setChecked(data.get(position).isState());
    }


    @Override
    public ItemServiceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemServiceHolder(layoutInflater.inflate(R.layout.item_order_service_sub, parent, false));
    }

    public class ItemServiceHolder extends BaseRecyclerAdapter.ViewHolder {
        private RadioButton rbItemServiceName;

        public ItemServiceHolder(View itemView) {
            super(itemView);
            rbItemServiceName = (RadioButton) itemView.findViewById(R.id.rb_item_service_name_sub);
        }

        public void bindData(final OnLineOrderServiceModel.DataBean.SubsBean model) {
            rbItemServiceName.setText(model.getServiceName());
            View.OnClickListener clickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (data.get(position).isState()) {

                        data.get(position).setState(false);
                    } else {
                        data.get(position).setState(true);
                    }
                    notifyItemRangeChanged(0, data.size());
                }
            };
            itemView.setOnClickListener(clickListener);
            rbItemServiceName.setOnClickListener(clickListener);
        }
    }


}
